<?php
/**
 * 期号表
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class IssuesNumber extends Model
{
    //set talbename
    protected $table = 'issues_number';
    //set primary key
    protected $pk = 'issues_no';

    /**
     * @desc 添加购物车
     * @return false|int
     */
    public function addIssuesNumber($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    /**
     * @desc 根据条件获取期号
     * @param $conds $fields
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getIssuesNumberByCond($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            if (in_array($val, $this->field)) {
                $arrField[] = $val;
            }
        }
        empty($arrField) && $arrField = $this->field;

        return $this->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    public function getCount($conds = [], $field = 'issues_no')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';
        return Db::table($this->table)->where($arrCond)->count($field);
    }

    /**
     * @desc 编辑期号
     * @param $cart_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function editIssuesNumber($issues_no, $param)
    {
        $conds = array(
            'issues_no' => $issues_no,
        );
        $field = [];
        if (isset($param['volume'])) {
            $field['volume'] = $param['volume'];
        }
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @desc 删除期号
     * @param $cart_id
     * @return int
     */
    public function deleteIssuesNumber($issues_no)
    {
        $conds = array(
            'issues_no' => $issues_no,
        );
        return Db::table($this->table)->where($conds)->delete();
    }

    public function getStartTimeAttr($value)
    {
        return date("Y-m-d H:i:s", $value);
    }

    public function getEndTimeAttr($value)
    {
        return date("Y-m-d H:i:s", $value);
    }
}
